package com.xiaohudie.SGGJUC;

import java.util.concurrent.TimeUnit;

/**
 * @Author xiao hu die
 * @Date 2022/3/6 23:45
 * @Version 1.0
 */
/**synchronized锁的是当前对象
 ** 没加synchronized的方法不是同步方法，不受锁的影响
 ** synchronized修饰静态方法的时候就是锁的是整个对象
 ** 当有一个静态方法与一个一般同步方法同时被调用了之后就会出现不是同一把锁的问题*/
public class TestSynchronized {
    public static void main(String[] args) {
        Phone phone = new Phone();
        Phone phone1 = new Phone();
        new Thread(new Runnable() {
            @Override
            public void run() {
                phone.sayHai();
            }
        }, "AA").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                phone1.sayWTF();
            }
        }, "BB").start();
    }

    static class Phone {
        public synchronized void sendMessage() {
            System.out.println("发短信");
        }

        public synchronized void sendPhone() {
            try {
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("打电话");
        }

        public void sayHell0() {
            System.out.println("Hello");
        }

        public static synchronized void sayHai() {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Hi");
        }

        public static synchronized void sayWTF() {
            System.out.println("WTF");
        }
        public synchronized void sayWind(){
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("HelloWind");
        }
    }
}
